package io.renren.modules.jsf.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.renren.modules.jsf.entity.vo.CourseVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.jsf.entity.MembertypeEntity;
import io.renren.modules.jsf.service.MembertypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * @author chen
 * @email sun@gmail.com
 * @date 2022-03-31 00:37:02
 */
@RestController
@RequestMapping("jsf/membertype")
public class MembertypeController {
    @Autowired
    private MembertypeService membertypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jsf:membertype:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = membertypeService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list1")
    public R list1(@RequestParam Map<String, Object> params) {
        List<MembertypeEntity> list = membertypeService.selectAll();
        List<Object> data = list.stream().map(t -> {
            CourseVo courseVo = new CourseVo();
            courseVo.setValue(t.getTypename());
            courseVo.setLabel(t.getTypename());
            return courseVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{typeid}")
    @RequiresPermissions("jsf:membertype:info")
    public R info(@PathVariable("typeid") Integer typeid) {
        MembertypeEntity membertype = membertypeService.getById(typeid);

        return R.ok().put("membertype", membertype);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jsf:membertype:save")
    public R save(@RequestBody MembertypeEntity membertype) {
        membertypeService.save(membertype);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jsf:membertype:update")
    public R update(@RequestBody MembertypeEntity membertype) {
        membertypeService.updateById(membertype);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jsf:membertype:delete")
    public R delete(@RequestBody Integer[] typeids) {
        membertypeService.removeByIds(Arrays.asList(typeids));

        return R.ok();
    }

}
